package com.ruoyi.background.service.impl;

import com.ruoyi.background.domain.AppGradeRecord;
import com.ruoyi.background.domain.AppLotteryPrize;
import com.ruoyi.background.domain.AppWechatUser;
import com.ruoyi.background.mapper.AppGradeRecordMapper;
import com.ruoyi.background.mapper.AppLotteryRecordMapper;
import com.ruoyi.background.mapper.AppWechatUserMapper;
import com.ruoyi.background.service.BuchangService;
import com.ruoyi.background.service.IAppLotteryPrizeService;
import com.ruoyi.background.utils.CONFIG_KEY;
import com.ruoyi.background.utils.REDIS_KEY;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.service.ISysConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;


@Service
public class BuchangImp implements BuchangService {


    @Autowired
    private AppWechatUserMapper appWechatUserMapper;

    @Autowired
    private AppLotteryRecordMapper appLotteryRecordMapper;

    @Autowired
    private AppGradeRecordMapper appGradeRecordMapper;

    @Autowired
    private RedisCache redisCache;

    @Autowired
    private ISysConfigService sysConfigService;

    @Autowired
    private IAppLotteryPrizeService appLotteryPrizeService;

    /**
     * 积分补偿1002
     */
    @Override
    public void buchang1002() {
        List<AppWechatUser> wechatUserList = appWechatUserMapper.selectAppWechatUserList(new AppWechatUser());
        for (AppWechatUser wechatUser : wechatUserList) {
            if (wechatUser.getUserId() != null) {
                Integer count = appLotteryRecordMapper.countByUserIdAndIndex(wechatUser.getUserId(), 2);
                if (count == null || count == 0) {
                    //插入积分消耗的记录
                    AppGradeRecord appGradeRecord = new AppGradeRecord();
                    appGradeRecord.setUserId(wechatUser.getUserId());
                    appGradeRecord.setType(3);
                    appGradeRecord.setGrade(1000);
                    appGradeRecord.setBeforeGrade(wechatUser.getGrade());
                    appGradeRecord.setRemark("系统1001补偿");
                    appGradeRecord.setCreateTime(DateUtils.getNowDate());
                    appGradeRecordMapper.insertAppGradeRecord(appGradeRecord);
                    //修改用户的积分
                    AppWechatUser updateUser = new AppWechatUser();
                    updateUser.setId(wechatUser.getId());
                    updateUser.setGrade(wechatUser.getGrade() + 1000);
                    appWechatUserMapper.updateAppWechatUser(updateUser);
                    System.out.println(wechatUser.getPhoneNumber() + "积分补偿");
                } else {
                    System.out.println(wechatUser.getPhoneNumber() + "中奖，不积分补偿");
                }
            } else {
                System.out.println(wechatUser.getOpenid() + "未绑定微信账号");
            }
        }
    }


    /**
     * 第一步
     */
    @Override
    public void buchang1010() {
        // 第一步 刷新奖品数量
        int num1 = 0;
        int num2 = 0;
        String nowWinNumKey = REDIS_KEY.APP_LOTTERY_WINNING_NUM;
        Integer integer = appLotteryRecordMapper.countWinNumLotteryByIndex(2);
        int nowWinNum = integer == null ? 0 : integer;
        redisCache.setCacheObject(nowWinNumKey, nowWinNum);
        System.out.println("刷新缓存中奖数量：" + nowWinNum);
        //必中的奖品
        Long winId = Long.valueOf(sysConfigService.selectConfigByKey(CONFIG_KEY.LOTTERY_MUST_WIN_ID));
        AppLotteryPrize mustWinPrize = appLotteryPrizeService.selectAppLotteryPrizeById(winId);
        //不中的奖品
        Long notWinId = Long.valueOf(sysConfigService.selectConfigByKey(CONFIG_KEY.LOTTERY_NOT_WIM_ID));
        AppLotteryPrize notWinPrize = appLotteryPrizeService.selectAppLotteryPrizeById(notWinId);
        //第二部  补偿积分 中奖的每人补给1000，没中奖的1500 指定中奖的不退还
        List<AppWechatUser> userList = appWechatUserMapper.selectAppWechatUserList(new AppWechatUser());
        for (AppWechatUser appWechatUser : userList) {
            if (appWechatUser.getUserId() == null || ObjectUtils.isEmpty(appWechatUser.getUserId()))
                continue;
            Integer count = appLotteryRecordMapper.countByUserIdAndIndex(appWechatUser.getUserId(), 2);
//            int grade;
//            if (count == null || count == 0) {
//                //没中奖的1500
//                grade = 1500;
//            } else {
//               grade = 1000;
//            }
//            Integer integer1 = appGradeRecordMapper.countByUserId(appWechatUser.getUserId());
//            if (ObjectUtils.isEmpty(integer1) || integer1 == 0){
//                AppGradeRecord appGradeRecord = new AppGradeRecord();
//                appGradeRecord.setUserId(appWechatUser.getUserId());
//                appGradeRecord.setType(3);
//                appGradeRecord.setGrade(grade);
//                appGradeRecord.setBeforeGrade(appWechatUser.getGrade());
//                appGradeRecord.setRemark("系统1010补偿");
//                appGradeRecord.setCreateTime(DateUtils.getNowDate());
//                appGradeRecordMapper.insertAppGradeRecord(appGradeRecord);
//                //修改用户的积分
//                AppWechatUser updateUser = new AppWechatUser();
//                updateUser.setId(appWechatUser.getId());
//                updateUser.setGrade(appWechatUser.getGrade() + grade);
//                appWechatUserMapper.updateAppWechatUser(updateUser);
//                System.out.println(appWechatUser.getPhoneNumber() + "积分补偿：" + grade);
//            }else {
//                System.out.println(appWechatUser.getPhoneNumber()+"已经补偿");
//            }
            if (count == null || count == 0) {
                String perKey = REDIS_KEY.APP_LOTTERY_PRIZE_USER_PER + appWechatUser.getUserId();
                //没中奖的重新生成中奖的
                List<AppUserPrePrize> prePrizeList = new ArrayList<>();
                // 判断是否必中 不是必中 查询是否中奖
                AppUserPrePrize prePrize = new AppUserPrePrize();
                prePrize.setPrize(mustWinPrize);
                prePrize.setUserId(appWechatUser.getUserId());
                prePrize.setPrizeId(mustWinPrize.getId());
                System.out.println(appWechatUser.getPhoneNumber() + "未中奖比中奖");
                num1++;
//                for (int i = 0; i < 3; i++) {
//                    AppUserPrePrize prePrize = new AppUserPrePrize();
//                    if (0 == i) {
//                        prePrize.setPrize(mustWinPrize);
//                        prePrize.setUserId(appWechatUser.getUserId());
//                        prePrize.setPrizeId(mustWinPrize.getId());
//                        System.out.println(appWechatUser.getPhoneNumber() + "第" + i + "次中奖");
//                    } else {
//                        prePrize.setPrize(notWinPrize);
//                        prePrize.setUserId(appWechatUser.getUserId());
//                        prePrize.setPrizeId(notWinPrize.getId());
//                    }
//                    prePrizeList.add(prePrize);
//                }
                prePrizeList.add(prePrize);
                redisCache.deleteByKey(perKey);
                if (!ObjectUtils.isEmpty(prePrizeList)) {
                    redisCache.setCacheList(perKey, prePrizeList);
                    redisCache.expire(perKey, 60 * 24, TimeUnit.MINUTES);
                }
            } else {
                num2++;

            }
        }
        System.out.println("刷新缓存中奖数量：" + nowWinNum);
        System.out.println("已经中奖：" + num2);
        System.out.println("未中奖：" + num1);
    }


























}
